<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const username = ref('')
const password = ref('')
const confirmPassword = ref('')
const errorMessage = ref('')
const successMessage = ref('')

const handleRegister = () => {
  // 简单的表单验证
  if (!username.value || !password.value || !confirmPassword.value) {
    errorMessage.value = '请填写所有必填字段'
    successMessage.value = ''
    return
  }

  if (password.value !== confirmPassword.value) {
    errorMessage.value = '两次输入的密码不一致'
    successMessage.value = ''
    return
  }

  if (password.value.length < 6) {
    errorMessage.value = '密码长度不能少于6位'
    successMessage.value = ''
    return
  }

  // 检查用户是否已存在
  const existingUsers = JSON.parse(localStorage.getItem('users') || '[]')
  const userExists = existingUsers.some(user => user.username === username.value)
  
  if (userExists) {
    errorMessage.value = '用户名已存在'
    successMessage.value = ''
    return
  }

  // 模拟注册API调用
  setTimeout(() => {
    // 保存用户信息到localStorage
    existingUsers.push({
      username: username.value,
      password: password.value // 注意：实际应用中应该加密存储密码
    })
    localStorage.setItem('users', JSON.stringify(existingUsers))
    
    // 注册成功后显示提示信息并在2秒后跳转到登录页面
    successMessage.value = '注册成功！即将跳转到登录页面...'
    errorMessage.value = ''
    
    setTimeout(() => {
      router.push('/login')
    }, 2000)
  }, 1000)
}
</script>

<template>
  <div class="register-container">
    <div class="register-form">
      <h2>用户注册</h2>
      
      <div class="form-group">
        <label for="username">用户名</label>
        <input 
          id="username"
          v-model="username" 
          type="text" 
          placeholder="请设置用户名"
          @keyup.enter="handleRegister"
        >
      </div>
      
      <div class="form-group">
        <label for="password">密码</label>
        <input 
          id="password"
          v-model="password" 
          type="password" 
          placeholder="请设置密码（至少6位）"
          @keyup.enter="handleRegister"
        >
      </div>
      
      <div class="form-group">
        <label for="confirmPassword">确认密码</label>
        <input 
          id="confirmPassword"
          v-model="confirmPassword" 
          type="password" 
          placeholder="请再次输入密码"
          @keyup.enter="handleRegister"
        >
      </div>
      
      <button class="register-btn" @click="handleRegister">注册</button>
      
      <div class="login-link">
        已有账号？<router-link to="/login">立即登录</router-link>
      </div>
      
      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
      
      <div v-if="successMessage" class="success-message">
        {{ successMessage }}
      </div>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  position: relative;
  overflow: hidden;
}

/* 动画背景 */
.register-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(45deg, #3498db, #9c27b0, #35495e, #42b983);
  background-size: 400% 400%;
  animation: gradientBG 15s ease infinite;
  z-index: 1;
}

.register-form {
  background: rgba(255, 255, 255, 0.95);
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  width: 100%;
  max-width: 400px;
  position: relative;
  z-index: 2;
  backdrop-filter: blur(5px);
}

/* 渐变背景动画 */
@keyframes gradientBG {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.register-form h2 {
  text-align: center;
  margin-bottom: 24px;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #666;
  font-size: 14px;
}

.form-group input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  transition: border-color 0.3s;
}

.form-group input:focus {
  outline: none;
  border-color: #42b983;
}

.register-btn {
  width: 100%;
  padding: 12px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.register-btn:hover {
  background-color: #3aa876;
}

.login-link {
  text-align: center;
  margin-top: 16px;
  font-size: 14px;
  color: #666;
}

.login-link a {
  color: #42b983;
  text-decoration: none;
}

.login-link a:hover {
  text-decoration: underline;
}

.error-message {
  color: #f44336;
  font-size: 14px;
  text-align: center;
  margin-top: 16px;
}

.success-message {
  color: #42b983;
  font-size: 14px;
  text-align: center;
  margin-top: 16px;
}
</style>